/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.hibernate.HibernateUtil;
import br.gov.cemaden.library.interfaces.smp.LoadAverageInterface;
import br.gov.cemaden.library.model.smp.ExecHardware;
import br.gov.cemaden.library.model.smp.LoadAverage;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author paulo.dias
 */
public class LoadAverageDao extends GenericDao implements LoadAverageInterface {
    
    private Session session;
    
    public LoadAverageDao(Session session){
        this.session = session;
    }

    public List<LoadAverage> findAll() {
        
        Transaction tx = session.beginTransaction();
        List<LoadAverage> loadAverage = null;
        try {
            Query query = session.getNamedQuery("LoadAverage.findAll");
            loadAverage = (List<LoadAverage>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no LoadAverageFacade.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return loadAverage;
    }

    public LoadAverage findById(Long id) {
        
        Transaction tx = session.beginTransaction();
        LoadAverage loadAverage = null;
        try {
            Query query = session.getNamedQuery("LoadAverage.findById").setParameter("id", id);
            loadAverage = (LoadAverage) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no LoadAverageFacade.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return loadAverage;
    }

    public List<LoadAverage> findByLoad1min(Double load1min) {
        
        Transaction tx = session.beginTransaction();
        List<LoadAverage> loadAverage = null;
        try {
            Query query = session.getNamedQuery("LoadAverage.findByLoad1min").setParameter("load1min", load1min);
            loadAverage = (List<LoadAverage>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no LoadAverage.findByLoad1min: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return loadAverage;
    }

    public List<LoadAverage> findByLoad5min(Double load5min) {
        
        Transaction tx = session.beginTransaction();
        List<LoadAverage> loadAverage = null;
        try {
            Query query = session.getNamedQuery("LoadAverage.findByLoad5min").setParameter("load5min", load5min);
            loadAverage = (List<LoadAverage>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no LoadAverageFacade.findByLoad5min: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return loadAverage;
    }

    public List<LoadAverage> findByLoad15min(Double load15min) {
        
        Transaction tx = session.beginTransaction();
        List<LoadAverage> loadAverage = null;
        try {
            Query query = session.getNamedQuery("LoadAverage.findByLoad15min").setParameter("load15min", load15min);
            loadAverage = (List<LoadAverage>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no LoadAverageFacade.findByLoad15min: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return loadAverage;
    }

    public List<LoadAverage> findByExecHardware(ExecHardware execHardware) {
        
        Transaction tx = session.beginTransaction();
        List<LoadAverage> loadAverage = null;
        try {
            Query query = session.getNamedQuery("LoadAverage.findByExecHardware").setParameter("execHardware", execHardware);
            loadAverage = (List<LoadAverage>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no LoadAverageFacade.findByExecHardware: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return loadAverage;
    }

    public LoadAverage insert(LoadAverage loadAverage) {
        try {
            saveOrUpdate(this.session, loadAverage);
        } catch (Exception ex) {
            Logger.getLogger(LoadAverageDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return loadAverage;
    }

    public LoadAverage update(LoadAverage loadAverage) {
        try {
            saveOrUpdate(this.session, loadAverage);
        } catch (Exception ex) {
            Logger.getLogger(LoadAverageDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return loadAverage;
    }

    public void delete(LoadAverage loadAverage) {
        try {
            remove(this.session, loadAverage);
        } catch (Exception ex) {
            Logger.getLogger(LoadAverageDao.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}
